package com.jingyi.web.controller.wechat;

import com.jingyi.common.annotation.Anonymous;
import com.jingyi.common.annotation.CheckOpenId;
import com.jingyi.common.annotation.Log;
import com.jingyi.common.core.controller.BaseController;
import com.jingyi.common.core.domain.AjaxResult;
import com.jingyi.common.core.domain.entity.WxUser;
import com.jingyi.common.core.page.TableDataInfo;
import com.jingyi.common.enums.BusinessType;
import com.jingyi.framework.util.CodeGenerator;
import com.jingyi.system.domain.ImportData;
import com.jingyi.system.service.IWxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 微信患者用户
 *
 * @author jingyi
 */
@RestController
@RequestMapping("/wxchat/user")
public class WxUserController extends BaseController {
    @Autowired
    private IWxUserService wxUserService;

    @Autowired
    private CodeGenerator codeGenerator;

    /**
     * 根据条件分页查询患者数据
     *
     * @param user 患者信息
     * @return 患者数据集合信息
     */
    @PreAuthorize("@ss.hasPermi('wx:user:query')")
    @GetMapping("/list")
    public TableDataInfo list(WxUser user)
    {
        startPage();
        List<WxUser> list = wxUserService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 根据条件查询患者数据
     *
     * @param user 患者信息
     * @return 患者数据集合信息
     */
    @PreAuthorize("@ss.hasPermi('wx:user:query')")
    @PostMapping("/listAll")
    public AjaxResult listAll(@RequestBody WxUser user)
    {
        List<WxUser> list = wxUserService.selectUserList(user);
        return success(list);
    }

    /**
     * 通过患者ID查询患者
     *
     * @param userId 患者ID
     * @return 患者对象信息
     */
    @PreAuthorize("@ss.hasPermi('wx:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable Long userId)
    {
        return success(wxUserService.selectUserById(userId));
    }


    /**
     * 患者状态统计
     *
     * @return 统计信息
     */
    // @PreAuthorize("@ss.hasPermi('wx:user:query')")
    @GetMapping("/getStatistic")
    public AjaxResult getStatistic(){
        return success(wxUserService.getStatistic());
    }


    /**
     * 通过根据条件查询患者信息
     *
     * @param user 患者信息
     * @return 患者对象信息
     */
    @CheckOpenId
    @PostMapping("/getByMap")
    public AjaxResult getInfo(@Valid @RequestBody WxUser user, BindingResult bindResult)
    {
        if(bindResult.hasErrors()){
            return error(bindResult.getAllErrors().get(0).getDefaultMessage());
        }
        return success(wxUserService.getByMap(user));
    }

    /**
     * 患者信息导入
     *
     * @param importData 数据
     * @return 结果
     */
    @CheckOpenId
    @PostMapping("/importData")
    public AjaxResult importData(@Valid @RequestBody ImportData importData){
        try {
            importData.setCode(codeGenerator.nextCode("wx_user"));
            wxUserService.importData(importData);
            return success();
        }catch (RuntimeException e){
            return error(e.getMessage());
        }
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('wx:user:add')")
    @Log(title = "患者信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WxUser user)
    {
        user.setCreateBy(getUsername());
        return toAjax(wxUserService.insertUser(user));
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('wx:user:edit')")
    @Log(title = "患者信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxUser user)
    {
        user.setUpdateBy(getUsername());
        return toAjax(wxUserService.updateUser(user));
    }

    /**
     * 修改用户
     */
    @Log(title = "患者信息管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeState")
    public AjaxResult changeState(@RequestBody WxUser user)
    {
        user.setUpdateBy(getUsername());
        return toAjax(wxUserService.updateUserState(user));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('wx:user:remove')")
    @Log(title = "患者信息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(wxUserService.deleteUserByIds(userIds));
    }





}
